package jse.setup;

import java.io.File;
import java.io.IOException;
import java.util.List;

import jse.dao.Dao;
import jse.dao.Record;
import jse.ds.JseDataSource;
import jse.json.Json;
import jse.tpl.Tpl;
import jse.util.Fs;
import jse.util.JseMap;
import jse.util.Strings;

public class Main {

	public static void main(String[] args) throws IOException {
		String cs=Fs.read("classpath:jse.json");
		JseMap conf=Json.toMap(cs);
		JseMap setup=conf.getMap("setup");
		JseDataSource ds=new JseDataSource(conf.getMap("jdbc"));
		String base=setup.getString("base");//d:/work/setup/
		Dao dao=new Dao(ds);
		String db=setup.getString("db");
		Tpl tpl=Tpl.use().setBaseTemplatePath(base);
		List<Record> list=(List<Record>) dao.select("select table_name from information_schema.tables where table_schema='"+db+"'");
		for (int i = 0; i < list.size(); i++) {
			Record tbl=list.get(i);
			String table=tbl.getString("table_name");
			setupcode(base,tpl,tbl,table,setup);//安装code
			List<Record> cols=(List<Record>) dao.select("select column_name,column_comment,data_type from information_schema.columns where table_schema='"+db+"' and table_name='"+table+"'");
			cols.forEach(x->{
				if(Strings.isEmpty(x.getString("column_comment"))) {
					x.put("column_comment", x.getString("column_name"));
				}
			});
			List<JseMap> htmls=setup.getList("html");
			htmls.forEach(x->{
				setuphtml(base,tpl,tbl,cols, table,
						x.getString("type"),x.getString("name"),x.getString("path")
						);
			});
		}
		if(setup.getBoolean("view"))
		view(db,dao,tpl,base,setup);//视图创建
		
	}
	public static void view(String db,Dao dao,Tpl tpl,String base,JseMap setup) {
		List<Record> list1=(List<Record>) dao.select("SELECT * from information_schema.VIEWS where table_schema='"+db+"'");
		for (int i = 0; i < list1.size(); i++) {
			Record tbl=list1.get(i);
			String table=tbl.getString("table_name");
			setupcode(base,tpl,tbl,table,setup);//安装code
			List<Record> cols1=(List<Record>) dao.select("select column_name,column_comment,data_type from information_schema.columns where table_schema='"+db+"' and table_name='"+table+"'");
			cols1.forEach(x->{
				if(Strings.isEmpty(x.getString("column_comment"))) {
					x.put("column_comment", x.getString("column_name"));
				}
			});
			List<JseMap> htmls=setup.getList("html");
			htmls.forEach(x->{
				setuphtml(base,tpl,tbl,cols1, table,
						x.getString("type"),x.getString("name"),x.getString("path")
						);
			});
		}
	}
	
	public static void setuphtml(String base,Tpl tpl,Record tbl,List<Record> cols,String table,
			String type,String name,String path) {
		File buidir=new File(base+"/web/"+type+"/"+name+"/");
		File[] buis=buidir.listFiles();
		for (int j = 0; j < buis.length; j++) {
			String code=tpl.getTemplate("/web/"+type+"/"+name+"/"+buis[j].getName())
					.renderToString(JseMap.make("cols",cols)
							.set("table", table));
			try {
					File f=new File(base+"setup/web/"+type+"/"+name+path+"/"+table.replace("_", "/")+"/"+buis[j].getName());
					Fs.createNewFile(f);
					Fs.write(f, code);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	//安装code
	public static void setupcode(String base,Tpl tpl,Record tbl,String table,JseMap setup) {
		File fdir=new File(base+"code/");
		File[] fs=fdir.listFiles();
		for (int j = 0; j < fs.length; j++) {
			String code=tpl.getTemplate("/code/"+fs[j].getName())
					.renderToString(JseMap.make("table",table));
			List<String> codes=setup.getList("code");
			try {
				if(codes.contains("index")){
				File f=new File(base+"setup/WEB-INF/classes/"+table+"/"+fs[j].getName());
				Fs.createNewFile(f);
				Fs.write(f, code);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			try {
				if(codes.contains("admin")){
					File f1=new File(base+"setup/WEB-INF/classes/admin/"+table.replace("_", "/")+"/"+fs[j].getName());
					Fs.createNewFile(f1);
					Fs.write(f1, code);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}
